import config from '@/config/index.js'
import isPE from '@/utils/isPE'
export default function () {
    if (isPE()) {
        let peRedirectPath = this.$route.query.peRedirectPath
            //pe登录，获取不到window.opener，就调用这个地址，把token传过去
        let peHandleQQLoginPage = this.$route.query.peHandleQQLoginPage
        let params = {
            peRedirectPath,
            peHandleQQLoginPage
        }
        let paramsStr = []
        for (const k in params) {
            paramsStr.push(k + "=" + encodeURI(params[k]))
        }
        QC.Login.showPopup({
            appId: config.QQ_APPID,
            redirectURI: config.QQ_CALLBACK + "?"+ paramsStr.join("&")
        });
    } else {
        let cbName = "qqLoginCb" + new Date().getTime()
        let cbFun = e => {
            let obj = e.data[cbName]
            if (obj) {
                let callback = this.$route.query.callback
                if (callback) {
                    let msg = {}
                    msg[callback] = obj
                    window.opener.postMessage(msg, '*')
                    this.$nextTick(() => {
                        window.close()
                    })
                } else {
                    console.log("无callback")
                }
            } else {
                console.log("无callback")
            }
        }
        window.addEventListener('message', cbFun)
        QC.Login.showPopup({
            appId: config.QQ_APPID,
            redirectURI: config.QQ_CALLBACK + "?callback=" + cbName
        });
    }
}